package mplayer;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.gstreamer.Bus;
import org.gstreamer.GstObject;
import org.gstreamer.State;
import org.gstreamer.TagList;
import org.gstreamer.elements.PlayBin2;
import org.gstreamer.swing.VideoComponent;
import org.json.simple.JSONObject;

public class Player {
	
	private PlayBin2 		playbin_;
	private VideoComponent	vc_;
	private int 			volume_;
	private boolean 		isMuted_;
	private JSONObject 		songinfo_;
	
	public Player() {
		playbin_ 	= new PlayBin2("MyMS mplayer");
		vc_			= new VideoComponent();
		songinfo_ 	= new JSONObject();
		
		playbin_.setVideoSink(vc_.getElement());
		playbin_.setVolumePercent(50);
		isMuted_ = false;
		
		initBusCallback();
	}
	
	private void initBusCallback() {
		playbin_.getBus().connect(new Bus.EOS() {
			public void endOfStream(GstObject arg0) {
				// Notify Gui of EOS
			}
		});
		
		playbin_.getBus().connect(new Bus.STATE_CHANGED() {
			public void stateChanged(GstObject arg0, State arg1, State arg2, State arg3) {
				// Notify Gui of State changed
			}
		});
		
		playbin_.getBus().connect(new Bus.ERROR() {
			public void errorMessage(GstObject arg0, int arg1, String arg2) {
				// Error handling!
			}
		});
		
		playbin_.getBus().connect(new Bus.TAG() {
			@SuppressWarnings("unchecked")
			public void tagsFound(GstObject source, TagList tagList) {
				for (String tagName : tagList.getTagNames()) {
                    for (Object tagData : tagList.getValues(tagName)) {
                        if (tagName.equals("artist")) {
                        	songinfo_.put("artist", tagData);
                        }
                        if (tagName.equals("title")) {
                        	songinfo_.put("title", tagData);
                        }
                        if (tagName.equals("album")) {
                        	songinfo_.put("album", tagData);
                        }
                        if (tagName.equals("genre")) {
                        	songinfo_.put("genre", tagData);
                        }
                    }
                }
			}
		});
	}
	
	
	public void mute() {
		if (isMuted_) {
			setVolume(volume_);
		} else {
			volume_ = playbin_.getVolumePercent();
			setVolume(0);
		}
		isMuted_ = !isMuted_;
	}
	
	public void unmute() {
		
	}
	
	public void setVolume(int p) {
		playbin_.setVolumePercent(p);
	}
	
	public void ready() {
		playbin_.ready();
	}
	
	public void play() {	
		playbin_.play();
	}	
	public void pause() {
		State currentState = playbin_.getState();
		if (currentState == State.PLAYING) {
			playbin_.pause();
		} else {
			play();
		}
	}
	
	public void stop() {
		playbin_.stop();
	}
	
	public void seek(double p) {
		long duration = playbin_.queryDuration(TimeUnit.SECONDS);
		playbin_.seek((long) (duration*p), TimeUnit.SECONDS);
	}
	
	
	/**
	 * Returns the current position in time.
	 * @return the current time in seconds.
	 */
	protected long getQueryPos() {
		return playbin_.queryPosition(TimeUnit.SECONDS);
	}
	
	/**
	 * Returns the length of the media file.
	 * @return the duration in seconds
	 */
	protected long getQueryDur() {
		return playbin_.queryDuration(TimeUnit.SECONDS);
	}
	
	public void playFile(File f) {
		if (playbin_.getState() != State.NULL) {
			stop();
		}
		playbin_.setInputFile(f);
		play();
	}
	
	public VideoComponent getVideo() {
		return vc_;
	}
	
	public JSONObject getTags() {
		return songinfo_;
	}
}
